package caddey.jonathan.gameframeworkandroidimplementation;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import framework.GameContext;
import framework.Screen;

public class GameView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

	// see http://developer.android.com/guide/topics/graphics/2d-graphics.html
	// for description of how to use surfaceview
	private static final int FPS = 90;
	private static final int LOWEST_FPS = 20;
	private static final int LONGEST_FRAME_TIME = 1000 / LOWEST_FPS;
	
	private Thread my_thread;
	private long my_lastUpdateTime;
	
	private volatile boolean my_running;
	private SurfaceHolder my_holder;
	private GameContext my_game;
	private Bitmap my_bitmap;
	public GameView(final AndroidGameContext the_game, final Bitmap the_bitmap) {
		super(the_game);
		my_bitmap = the_bitmap;
		my_game = the_game;
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO donno if I should use this.
		
	}

	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		my_lastUpdateTime = System.currentTimeMillis();
		my_holder = holder;
		if (my_running) {
			my_thread = new Thread(this);
			my_thread.start();
		}
	}
	public void resume() {
		my_running = true;
		getHolder().addCallback(this);
		if (getHolder().getSurface().isValid()) {
			my_thread = new Thread(this);
			my_thread.start();			
		}
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
	}

	public void run() {
		Rect bounds = new Rect();
		while (my_running && my_holder != null) {
			
			
			final long time = System.currentTimeMillis();
			int deltaTime = (int) (time - my_lastUpdateTime);
			if (deltaTime >= LONGEST_FRAME_TIME) {
				deltaTime = LONGEST_FRAME_TIME;
			}
			my_lastUpdateTime = time;
			
			Canvas canvas = my_holder.lockCanvas();
			if (canvas == null) {
				break;
			}
			((AndroidGraphics)my_game.getGraphicsWrapper()).setCanvas(canvas);
			my_game.getCurrentScreen().step(deltaTime);
			my_game.getGraphicsWrapper().saveMatrix();
            my_game.getCurrentScreen().render();
            my_game.getGraphicsWrapper().restoreMatrix();
            
            canvas.getClipBounds(bounds);
            //canvas.drawBitmap(my_bitmap, null, bounds, null);
            //canvas.drawBitmap(my_bitmap, 0, 0, null);
            my_holder.unlockCanvasAndPost(canvas);
            

			try {
				Thread.sleep(1000 / FPS);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public void pause() {
		my_running = false;
		my_holder.removeCallback(this);
        while(true) {
            try {
                my_thread.join();
                break;
            } catch (InterruptedException e) {
                // retry
            }
        }
    }
	



}
